package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.server.mapper.MenuButtonMapper;
import com.ybkj.daijia.server.model.MenuButton;
import com.ybkj.daijia.server.service.MenuButtonService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DefaultMenuButtonService implements MenuButtonService {

    @Autowired
    private MenuButtonMapper menuButtonMapper;

    @Override
    public long countByParent(final Long parentID) {

        if (null == parentID) {
            return menuButtonMapper.countByParentIsNull();
        } else {
            return menuButtonMapper.countByParentId(parentID);
        }
    }

    @Override
    public MenuButton findOne(Long id) {

        return menuButtonMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MenuButton insertRecord(MenuButton button) {

        menuButtonMapper.insertRecord(button);

        return button;
    }

    @Override
    public List<MenuButton> findAll() {

        return menuButtonMapper.selectByAll();
    }

    @Override
    public List<MenuButton> findByParentIsNull() {

        return menuButtonMapper.selectByParentIsNull();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(MenuButton btn) {

        long total = menuButtonMapper.countByParentId(btn.getId());
        if (total > 0) {
            menuButtonMapper.deleteByParentId(btn.getId());
        }
        menuButtonMapper.deleteByPrimaryKey(btn.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(MenuButton record) {

        return menuButtonMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<MenuButton> findByParent(Long parentId) {

        return menuButtonMapper.selectByParentId(parentId);
    }

}
